﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using CommonDomainInterfaces;

namespace ServerConsole
{


    class ClientThreads
    {
        ServerToKlientCommands serverCommands = new ServerToKlientCommands();
        ClientToServerCommands klientCommands = new ClientToServerCommands();

        Auction owner;
        ClientsMonitor _Monitor;
        Socket _ClientSocket;
        bool bPendingShutDown = false;

        public ClientThreads(ClientsMonitor monitor, Socket clientSocket, Auction ownerauction)
        {
            _Monitor = monitor;
            _ClientSocket = clientSocket;
            owner = ownerauction;
        }

        public void HandleClient()
        {
            byte command;
            string ClientIP;
            string username = "";

            NetworkStream networkStream;
            BinaryReader reader;
            BinaryWriter writer;

            networkStream = new NetworkStream(_ClientSocket);
            reader = new BinaryReader(networkStream);
            writer = new BinaryWriter(networkStream);

            IPAddress ClientIPAdress = ((IPEndPoint)this._ClientSocket.RemoteEndPoint).Address;
            ClientIP = ClientIPAdress.ToString();
            IPHostEntry ip = Dns.GetHostEntry(ClientIPAdress);
            Thread.CurrentThread.Name = ip.HostName;

            _Monitor.AddWriter(writer);
            //_Monitor.BroadCastLogon(ClientIPAdress, "er nu med i auktionen");

            while (!bPendingShutDown)
            {
                try
                {
                    command = reader.ReadByte();

                    switch (command)
                    {
                        case (byte)ClientToServerCommands.SetUserName:
                            if (username != "")
                            {
                                username = reader.ReadString();//aflæs string fra strømen
                            }
                            break;

                        case (byte)ClientToServerCommands.GiveBid:
                            
                            if (username != "")
                            {
                                decimal registeredbid = reader.ReadDecimal();

                                Bid bidTosend = new Bid(registeredbid, username);
                                owner.AcceptNewBid(bidTosend);
                            }
                            break;

                        default:
                            // Throw InvalidCommandException Jeg har ikke skrevet denne exception endnu
                            break;
                    }
                }
                catch (IOException) //opstår når clienten ikke længere er logget på
                {
                    break;
                }
            }

            _Monitor.RemoveWriter(writer);

            networkStream.Close();
            writer.Close();
            reader.Close();
        }




    }
}
